home *** CD-ROM | disk | FTP | other *** search
- /* Main code for Dcal */
-
- /*
- DCal, a Discordian calendar for the Palm OS.
- Copyright (C) 1999 Ron Hale-Evans <rwhe@apocalypse.org>.
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; the version used for this program
- is Version 2.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
-
- This program comes with ABSOLUTELY NO WARRANTY. Use it at your own risk.
- For the text of the GNU General Public License, see the file gpl.txt,
- which should have been distributed with this file.
- */
-
-
- #include <Pilot.h>
- #include "callback.h"
- #include "dcalRsc.h"
-
-
- // Globals
-
- int dYear;
- int dMonth;
- int dDay;
- int dWeekday;
- int StTibsDayFlag;
- char TextToPrint [150];
-
-
- char DiscoMoNames[6][15] =
- {
- "ERROR",
- "Chaos",
- "Discord",
- "Confusion",
- "Bureaucracy",
- "Aftermath"
- };
-
- char DiscoWeekdayNames[5][20] =
- {
- "Sweetmorn",
- "Boomtime",
- "Pungenday",
- "Prickle-Prickle",
- "Setting Orange"
- };
-
- char DiscoApostleHolyDays[6][15] =
- {
- "ERROR",
- "Mungday",
- "Mojoday",
- "Syaday",
- "Zaraday",
- "Maladay"
- };
-
- char DiscoSeasonHolyDays[6][20] =
- {
- "ERROR",
- "Chaoflux",
- "Discoflux",
- "Confuflux",
- "Bureflux",
- "Afflux"
- };
-
- int DiscoMoLens[6][2];
-
-
- void DiscoDataInit (void)
- {
- int i;
- int j;
-
- for (i=1; i<=5; i++)
- for (j=0; j<=1; j++)
- {
- DiscoMoLens[i][j] = 73;
- };
- DiscoMoLens[1][1] = 74;
- DiscoMoLens[0][0] = 0;
- DiscoMoLens[0][1] = 0;
- }
-
-
-
- int GregPJDFromGregDate(int gYear, int gMonth, int gDay)
- {
- ULong DaysForNewYears;
- ULong DaysForGDate;
- DateType NewYearsDate;
- DateType gDate;
-
- NewYearsDate.year = gYear;
- NewYearsDate.month = 1;
- NewYearsDate.day = 1;
-
- gDate.year = gYear;
- gDate.month = gMonth;
- gDate.day = gDay;
-
- DaysForNewYears = DateToDays(NewYearsDate);
- DaysForGDate = DateToDays(gDate);
-
- return (DaysForGDate - DaysForNewYears + 1);
- }
-
-
- int GregYearIsLeapYear (int year)
- {
- return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
- }
-
-
-
- void CalcDiscoDate (void)
- {
- ULong TimeInSeconds;
- DatePtr gDateP;
- int LeapFlag;
- int dPJD;
- int AdjustedPJD;
- int NextMonthStart;
- int PrevDays = 0;
- int i;
-
- gDateP = (DatePtr)MemPtrNew(sizeof(DateType));
- TimeInSeconds = TimGetSeconds();
- DateSecondsToDate (TimeInSeconds, gDateP);
-
- LeapFlag = 0;
- if (GregYearIsLeapYear(gDateP->year))
- LeapFlag = 1;
-
- dPJD = GregPJDFromGregDate(gDateP->year, gDateP->month, gDateP->day);
- AdjustedPJD = dPJD;
- if (LeapFlag)
- {
- if (dPJD == 60)
- StTibsDayFlag = true;
- else if (dPJD > 60)
- AdjustedPJD = dPJD - 1;
- };
- dWeekday = (AdjustedPJD-1) % 5;
-
- dMonth = 0;
- NextMonthStart = 1;
- while (dMonth < 5)
- {
- NextMonthStart = NextMonthStart + DiscoMoLens[dMonth][LeapFlag];
- if (AdjustedPJD >= NextMonthStart)
- dMonth++;
- else
- break;
- };
-
- for (i=1; i<=dMonth; i++)
- {
- PrevDays = PrevDays + DiscoMoLens[i-1][0];
- };
- dDay = AdjustedPJD - PrevDays;
-
- dYear = gDateP->year + 1166 + 1904;
-
- MemPtrFree (gDateP);
- }
-
-
- void MakeDiscoDateString (void)
- {
- char SeasonStr [20];
- char WeekdayStr [20];
- char YearStr [20];
- char DayStr [20];
- char HolyDay [20];
- int HolyDayFlag;
-
- StrCopy(SeasonStr,DiscoMoNames [dMonth]);
- StrCopy (WeekdayStr, DiscoWeekdayNames [dWeekday]);
- StrIToA (YearStr, dYear);
-
- HolyDayFlag = true;
-
- if (dDay == 5)
- StrCopy (HolyDay, DiscoApostleHolyDays[dMonth]);
- else if (dDay == 50)
- StrCopy (HolyDay, DiscoSeasonHolyDays[dMonth]);
- else
- HolyDayFlag = false;
-
- if (StTibsDayFlag)
- {
- StrCopy (TextToPrint, "St. Tib's Day,\n");
- StrCat (TextToPrint, YearStr);
- }
- else if (HolyDayFlag)
- {
- StrCopy (TextToPrint, HolyDay);
- StrCat (TextToPrint, ",\n");
- StrCat (TextToPrint, WeekdayStr);
- StrCat (TextToPrint, ",\n");
- StrIToA (DayStr, dDay);
- StrCat (TextToPrint, DayStr);
- StrCat (TextToPrint, " ");
- StrCat (TextToPrint, SeasonStr);
- StrCat (TextToPrint, " ");
- StrCat (TextToPrint, YearStr);
- }
- else
- {
- StrCopy (TextToPrint, WeekdayStr);
- StrCat (TextToPrint, ",\n");
- StrIToA (DayStr, dDay);
- StrCat (TextToPrint, DayStr);
- StrCat (TextToPrint, " ");
- StrCat (TextToPrint, SeasonStr);
- StrCat (TextToPrint, " ");
- StrCat (TextToPrint, YearStr);
- };
- }
-
-
-
-
-
- void ShowText (void)
- {
- FormPtr frmP;
- FieldPtr dstP;
- CharPtr dstT;
- VoidHand dstHandle;
-
- frmP = FrmGetActiveForm();
-
- dstP = (FieldPtr)(FrmGetObjectPtr(frmP, (FrmGetObjectIndex(frmP, MainTextField))));
-
- dstHandle = MemHandleNew(StrLen(TextToPrint)+1);
- dstT = MemHandleLock (dstHandle);
-
- StrCopy (dstT, TextToPrint);
- MemHandleUnlock (dstHandle);
- FldSetTextHandle (dstP, (Handle)dstHandle);
- FldDrawField (dstP);
- }
-
-
-
- void DisplayDate (void)
- {
- DiscoDataInit();
- CalcDiscoDate();
- MakeDiscoDateString();
-
- ShowText();
- }
-
-
-
-
- static Boolean AboutFormHandleEvent(EventPtr e)
- {
- Boolean handled = false;
-
- CALLBACK_PROLOGUE
-
- switch (e->eType)
- {
- case frmOpenEvent:
- FrmDrawForm(FrmGetActiveForm());
- handled = true;
- break;
- case ctlSelectEvent:
- if (e->data.popSelect.controlID == AboutOKButton)
- {
- FrmReturnToForm(MainForm);
- handled = true;
- break;
- }
- }
-
- CALLBACK_EPILOGUE
-
- return(handled);
- }
-
-
-
-
-
- static Boolean MainFormHandleEvent (EventPtr e)
- {
- Boolean handled = false;
- FormPtr frm;
-
- CALLBACK_PROLOGUE
-
- switch (e->eType) {
- case frmOpenEvent:
- frm = FrmGetActiveForm();
- FrmDrawForm(frm);
-
- DisplayDate();
-
- handled = true;
- break;
-
- case menuEvent:
- MenuEraseStatus(NULL);
-
- switch(e->data.menu.itemID)
- {
- case MainOptionsAboutMenuItem:
- FrmPopupForm(AboutForm);
- handled = true;
- break;
- }
-
- handled = true;
- break;
-
- case ctlSelectEvent:
- switch(e->data.ctlSelect.controlID) {
- }
- break;
-
- default:
- break;
- }
-
- CALLBACK_EPILOGUE
-
- return handled;
- }
-
-
-
- static Boolean ApplicationHandleEvent(EventPtr e)
- {
- FormPtr frm;
- Word formId;
- Boolean handled = false;
-
- if (e->eType == frmLoadEvent) {
- formId = e->data.frmLoad.formID;
- frm = FrmInitForm(formId);
- FrmSetActiveForm(frm);
-
- switch(formId) {
- case MainForm:
- FrmSetEventHandler(frm, MainFormHandleEvent);
- break;
- case AboutForm:
- FrmSetEventHandler(frm, AboutFormHandleEvent);
- break;
- }
- handled = true;
- }
-
- return handled;
- }
-
-
-
- /* Get preferences, open (or create) app database */
- static Word StartApplication(void)
- {
- FrmGotoForm(MainForm);
- return 0;
- }
-
-
-
-
- /* Save preferences, close forms, close app database */
- static void StopApplication(void)
- {
- FrmSaveAllForms();
- FrmCloseAllForms();
- }
-
-
-
-
- /* The main event loop */
- static void EventLoop(void)
- {
- Word err;
- EventType e;
-
- do {
- EvtGetEvent(&e, evtWaitForever);
- if (! SysHandleEvent (&e))
- if (! MenuHandleEvent (NULL, &e, &err))
- if (! ApplicationHandleEvent (&e))
- FrmDispatchEvent (&e);
- } while (e.eType != appStopEvent);
- }
-
-
-
-
- /* Main entry point; it is unlikely you will need to change this except to
- handle other launch command codes */
- DWord PilotMain(Word cmd, Ptr cmdPBP, Word launchFlags)
- {
- Word err;
-
- if (cmd == sysAppLaunchCmdNormalLaunch) {
-
- err = StartApplication();
- if (err) return err;
-
- EventLoop();
- StopApplication();
-
- } else {
- return sysErrParamErr;
- }
-
- return 0;
- }
-
-
-